package com.xysd.express.activity.expressage;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.google.gson.Gson;
import com.xysd.express.R;
import com.xysd.express.adapter.CargoTypeAdpter;
import com.xysd.express.base.BaseActivity;
import com.xysd.express.bean.CargoTypeBus;
import com.xysd.express.bean.CargoTypeObj;
import com.xysd.express.rx.RxBus;
import com.xysd.express.util.ActivityTaskManeger;
import com.xysd.express.util.JSONParser;
import com.xysd.express.util.ToastUtil;
import com.xysd.express.util.http.HttpCallBack;
import com.xysd.express.util.http.HttpUtil;

import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.ViewInject;

import java.util.ArrayList;
import java.util.List;

@ContentView(R.layout.activity_selecte_expressage_type)
public class SelecteExpressageTypeActivity extends BaseActivity implements View.OnClickListener {

    @ViewInject(R.id.type_rv)
    private RecyclerView type_rv;
    @ViewInject(R.id.cargoName_et)
    private EditText cargoName_et;
    @ViewInject(R.id.remark_et)
    private EditText remark_et;
    @ViewInject(R.id.comfirm_btn)
    private Button comfirm_btn;

    public static CargoTypeAdpter adapter;
    List<CargoTypeObj.CargoType> datalist;

    int typeId;
    String cargoName,remark;

    @Override
    protected void init() {
        setTitleBar("选择类型");
        comfirm_btn.setOnClickListener(this);
        datalist=new ArrayList<>();
        adapter=new CargoTypeAdpter(R.layout.item_cargotype,mActivity);
        type_rv.setLayoutManager(new GridLayoutManager(mActivity,3));
        type_rv.setAdapter(adapter);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                SelecteExpressageTypeActivity.adapter.setCurSelect(position);
                typeId=datalist.get(position).Id;
            }
        });


        getData();
    }

    private void getData() {
        HttpUtil.getCargoType(new HttpCallBack() {
            @Override
            public void onSuccess(String result) {
                Log.i("物品类型",result);
                String code = JSONParser.getStringFromJsonString("Status",result);
                String msg = JSONParser.getStringFromJsonString("Msg",result);
                if ("0".equals(code)){
                    CargoTypeObj obj=new Gson().fromJson(result, CargoTypeObj.class);
                    datalist=obj.Data;
                    adapter.setNewData(datalist);
                }else {
                    ToastUtil.show(msg);
                }
            }

            @Override
            public void onFailure(String error) {
                Log.e("物品类型",error);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.comfirm_btn:
                cargoName=cargoName_et.getText().toString();
                remark=remark_et.getText().toString();
                if (TextUtils.isEmpty(cargoName)){
                    ToastUtil.show("您还未填物品名称");
                    break;
                }
                if (TextUtils.isEmpty(remark)){
                    ToastUtil.show("您还未填备注");
                    break;
                }
                if (typeId==0){
                    ToastUtil.show("您还未选择物品类型");
                    break;
                }

                RxBus.getInstance().post(new CargoTypeBus(typeId,cargoName,remark));
                ActivityTaskManeger.getInstance().closeActivity(mActivity);
                break;
        }
    }
}
